import requests
from bs4 import BeautifulSoup, element
import os
import json

def downloadPages():
    if not os.path.exists('pages'):
        os.makedirs('pages')

    url = "https://wiki.biligame.com/dsp/%E9%A6%96%E9%A1%B5"

    page = requests.get(url)
    with open('pages/home.html', 'wb') as f:
        f.write(page.content)

    soup = BeautifulSoup(page.content, "html.parser")
    elements = soup.select('span.icon-normal')
    for element in elements:
        title = element.select('a')[0].attrs['title']
        print('download ' + title)
        link = 'https://wiki.biligame.com'+element.select('a')[0].attrs['href']
        page= requests.get(link)
        with open('pages/'+title+'.html', 'wb') as f:
            f.write(page.content)
    


def parserItems():
    plantList = set()

    jsonContent = None
    with open('iconmap.json', 'rb') as f:
        jsonContent = json.load(f)

    images = {}
    for item in jsonContent:
        images[item[1]] = item[0]+'.png'

    # print(images)
    # return

    items = []
    content = b''
    with open('pages/home.html', 'rb') as f:
        content = f.read()
    soup = BeautifulSoup(content, 'html.parser')
    elements = soup.select('span.icon-normal')
    for element in elements:
        title = element.select('a')[0].attrs['title']

        with open('pages/'+title+'.html', 'rb') as f:
            content = f.read()

        soup = BeautifulSoup(content, 'html.parser')
        table = soup.select('table.wikitable-dsp')
        formulas = soup.select('table.wikitable-dsp')[1].select('span.formula')
        itemFormulas = []
        type = []

        rows = soup.select('table.wikitable-dsp')[0].select('tr')
        for row in rows:
            if row.th.text.strip() == '分类':
                type = row.td.text.strip().split('、') 
        
        plants = []
        rows = table[1].select('tbody')[0].select('tr')
        if len(rows) > 1:
            for i in range(1, len(rows)):
                plant = rows[i].select('td')[1].text.strip()
                plantList.update(set((plant,)))
                plants.append(plant)

        for index, formula in enumerate(formulas):
            materials = {}
            products = {}
            cost = 0
            isMaterial = True

            badFormula = False
            for content in formula.contents:
                if ('formula-arrow' in content['class']):
                    t = content.select('span.formula-arrow-t-time')[0].text.strip()
                    if t == '?' or t == '？':
                        badFormula = True
                        break
                    cost = float(t[:-1])
                    isMaterial = False
                    continue
            
                if isMaterial:
                    text = '1'
                    t = content.select('span.icon-num')
                    if len(t) > 0:
                        text = t[0].text
                    t =content.select('a')[0].attrs['title']
                    if t == '硅块':
                        t = '高纯硅块'
                    if '制造台' in t:
                        t = t.upper()
                    materials[t] = int(text)
                else:
                    text = '1'
                    t = content.select('span.icon-num')
                    if len(t) > 0:
                        text = t[0].text
                    t =content.select('a')[0].attrs['title']
                    if t == '硅块':
                        t = '高纯硅块'
                    if '制造台' in t:
                        t = t.upper()
                    products[t] = int(text)
            if title not in products:
                continue
            if badFormula:
                continue
            itemFormulas.append({'matter': materials, 'cost': cost, 'plant': plants[index], 'product': products})

        if title not in images:
            print('cant find '+ title)
            continue
        print(title, plants)
        items.append({'name': title, 'image':images[title], 'type':type, 'from':itemFormulas})

    print(plantList) 
    with open('物品查看器/downloadrecipe.json', 'w', encoding='utf-8') as f:
        json.dump({'items':items}, f, indent=4, separators=(',', ':'), ensure_ascii=False)

# downloadPages()

parserItems()
